package com.lw.mall.web.lw;

import com.lw.mall.entity.User;
import com.lw.mall.entity.pojo.ResultBean;
import com.lw.mall.service.exception.LoginException;
import com.lw.mall.service.exception.LoginJsonException;
import com.lw.mall.service2.EvaluationService2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 评价
 */
@Slf4j
@Controller
public class ContextController {

    @Autowired
    private EvaluationService2 evaluationService2;
    @PostMapping("product/context")
    public void context(String context, HttpServletRequest request, Integer productId, HttpServletResponse response) throws IOException, ServletException {

        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            throw new LoginException("请登录！");
        }
        evaluationService2.saveContext(context,user,productId);
        //  /在服务器端是http://ip:port/工程路径   /在浏览器端只有http://ip:port
        response.sendRedirect("/mall/product/get.html?id="+productId);
//        return "forward:/product/get.html?id="+productId; //请求转发
    }

    //最终版
    @PostMapping("product/context2")
    @ResponseBody
    public ResultBean context2(String context, HttpServletRequest request, Integer productId){

        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            throw new LoginJsonException("请登录!");
        }
        evaluationService2.saveContext(context,user,productId);
        return new ResultBean(true);
    }

}
